﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using SPS.ImageAttachementFieldType;

namespace SPS.ImageAttachementFieldType.WebControls
{
    class ImageAttachementFieldControl : UrlField
    {
        protected Image hlImageAttachementField;

        protected override string DefaultTemplateName
        {
            get
            {
                if (this.ControlMode == SPControlMode.Display)
                {
                    return this.DisplayTemplateName;
                }
                else
                {
                    return "ImageAttachementFieldControl";
                }
            }
        }

        public override string DisplayTemplateName
        {
            get
            {
                return "ImageAttachementFieldControlForDisplay";
            }
            set
            {
                base.DisplayTemplateName = value;
            }
        }

        protected override void CreateChildControls()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                //activate event receiver feature by its ID
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    if (site.Features[new Guid("4c143237-a186-4ab1-b3b9-25ae35e55d8b")] == null)
                    {
                        site.AllowUnsafeUpdates = true;
                        site.RootWeb.AllowUnsafeUpdates = true;
                        site.Features.Add(new Guid("4c143237-a186-4ab1-b3b9-25ae35e55d8b"));
                    }
                }
            });
            if (this.Field != null)
            {
                // Make sure inherited child controls are completely rendered.
                base.CreateChildControls();

                // Associate child controls in the .ascx file with the 
                // fields allocated by this control.
                this.textBox = (TextBox)TemplateContainer.FindControl("UrlFieldDescription");
                this.urlBox = (TextBox)TemplateContainer.FindControl("UrlFieldUrl");
                this.hlImageAttachementField = (Image)TemplateContainer.FindControl("hlImageAttachementField");

                switch (this.ControlMode)
                {
                    case SPControlMode.New:
                        if (!Page.IsPostBack)
                            this.textBox.Text = string.Empty;

                        if (this.FieldName == "Picture")
                        {
                            this.Controls.Add(new LiteralControl("<script type='text/javascript' src='/_layouts/SPSImageAttachementFieldType/Scripts/jquery-1.6.2.js'></script>"));
                            this.Controls.Add(new LiteralControl("<script type='text/javascript' src='/_layouts/SPSImageAttachementFieldType/Scripts/AddScript.js'></script>"));
                            this.Controls.Add(new LiteralControl("<script type='text/javascript'>var ImageAttachementFieldControlID='" + this.urlBox.ClientID + "';</script>"));
                        }
                        break;
                    case SPControlMode.Edit:
                        if (this.ItemFieldValue != null)
                        {
                            this.textBox.Text = ((SPFieldUrlValue)this.ItemFieldValue).Description;
                            this.urlBox.Text = ((SPFieldUrlValue)this.ItemFieldValue).Url;
                        }
                        if (this.FieldName == "Picture")
                        {
                            this.Controls.Add(new LiteralControl("<script type='text/javascript' src='/_layouts/SPSImageAttachementFieldType/Scripts/jquery-1.6.2.js'></script>"));
                            this.Controls.Add(new LiteralControl("<script type='text/javascript' src='/_layouts/SPSImageAttachementFieldType/Scripts/UpdateScript.js'></script>"));
                            this.Controls.Add(new LiteralControl("<script type='text/javascript'>var ImageAttachementFieldControlID='" + this.urlBox.ClientID + "';</script>"));
                        }
                        break;
                    case SPControlMode.Display:
                        if (this.ItemFieldValue != null)
                        {
                            this.hlImageAttachementField.ImageUrl = ((SPFieldUrlValue)this.ItemFieldValue).Url;
                            this.hlImageAttachementField.AlternateText = ((SPFieldUrlValue)this.ItemFieldValue).Description;
                            this.hlImageAttachementField.Visible = true;
                        }
                        else
                        {
                            //hide image
                            this.hlImageAttachementField.Visible = false;
                        }
                        break;

                }
            }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return base.Value;
            }
            set
            {
                EnsureChildControls();
                base.Value = (SPFieldUrlValue)value;
            }
        }
    }
}
